    package cn.edu.svtcc.controller;
    import cn.edu.svtcc.config.StudentConfig;
    import cn.edu.svtcc.domain.Result;
    import cn.edu.svtcc.domain.Student;
    import cn.edu.svtcc.util.PropertiesSaver;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.BeanUtils;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.atomic.AtomicInteger;
    import org.springframework.beans.BeanWrapper;
    import org.springframework.beans.BeanWrapperImpl;
    import java.beans.FeatureDescriptor;


    @RestController
    public class StudentController {

        private final StudentConfig studentConfig;
        private final AtomicInteger counter;

        public StudentController(StudentConfig studentConfig) {
            this.studentConfig = studentConfig;
            // 初始化 counter，从现有最大号开始
            int max = studentConfig.getList().stream()
                    .mapToInt(s -> Integer.parseInt(s.getNumber()))
                    .max().orElse(1000);
            this.counter = new AtomicInteger(max);
        }

        @GetMapping("/students")
        public Result getStudent() {
            return Result.success(studentConfig.getList());
        }

        @PostMapping("/students")
        public Result create(@RequestBody Student student) throws Exception {
            student.setNumber(String.valueOf(counter.incrementAndGet()));
            studentConfig.getList().add(student);
            PropertiesSaver.saveAllData(studentConfig.getList(), studentConfig.getGrades().getList());
            Result update = new Result();
            update.setStatus(201);
            update.setMsg("create");
            update.setData(student);
            return update;
        }

        @PutMapping("/students")
        public Result update(@RequestBody Student student) throws Exception {
            List<Student> list = studentConfig.getList();
            for (int i = 0; i < list.size(); i++) {
                Student existing = list.get(i);
                if (existing.getNumber().equals(student.getNumber())) {
                    // 仅复制非空字段（排除number）
                    String[] nullFields = getNullPropertyNames(student);
                    String[] ignoredFields = Arrays.copyOf(nullFields, nullFields.length + 1);
                    ignoredFields[ignoredFields.length - 1] = "number";
                    BeanUtils.copyProperties(student, existing, ignoredFields);

                    PropertiesSaver.saveAllData(studentConfig.getList(), studentConfig.getGrades().getList());
                    return Result.success(200, "update", existing);
                }
            }
            return Result.error(404, "学生不存在");
        }

        // 新增工具方法
        private String[] getNullPropertyNames(Student source) {
            BeanWrapper src = new BeanWrapperImpl(source);
            return Arrays.stream(src.getPropertyDescriptors())
                    .map(FeatureDescriptor::getName)
                    .filter(name -> src.getPropertyValue(name) == null)
                    .toArray(String[]::new);
        }

        @DeleteMapping("/students/{number}")
        public Result delete(@PathVariable String number) throws Exception {
            List<Student> list = studentConfig.getList();
            Student removed = null;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getNumber().equals(number)) {
                    removed = list.remove(i);
                    break;
                }
            }
            if (removed != null) {
                PropertiesSaver.saveAllData(studentConfig.getList(), studentConfig.getGrades().getList());

                return Result.success(removed);
            }
            return Result.success("学生不存在或者已经被删除");
        }
    }
